package controller.teacher;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import pojos.common.ReplyMessage;
import pojos.subject.ClassInfo;
import pojos.teacher.TeacherInfo;
import service.CommonUtilsService;
import service.TeacherInfoService;
import utils.InputVerifyUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

/**
 * 教师信息控制器
 */
@Controller
@RequestMapping(value = "/teacher")
public class TeacherInfoController {

    @Resource(name = "teacherInfoService")
    private TeacherInfoService teacherInfoService;

    @Resource(name = "commonUtilsService")
    private CommonUtilsService utilsService;

    /**
     * 教师修改基本信息提交
     */
    @PostMapping("/frame/alterBase-submit")
    @ResponseBody
    public ReplyMessage studentAlterBaseInfo(@RequestParam(value = "realName",required = false) String realName,
                                             @RequestParam(value = "sex",required = false) Integer sex,
                                             @RequestParam(value = "school",required = false) String school,
                                             @RequestParam(value = "grade",required = false) Integer grade,
                                             @RequestParam(value = "edu",required = false) Integer edu,
                                             HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();
        TeacherInfo teacherInfo = (TeacherInfo) session.getAttribute("UserInfo");

        // 真实姓名验证
        if(realName != null && realName.length() > 0){
            if(InputVerifyUtils.isRealNameNotRight(realName)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("所填写的姓名不正确！");
                return replyMessage;
            }
            teacherInfo.setRealName(realName);
        }

        // 学校验证
        if(school != null && school.length() > 0){
            if(InputVerifyUtils.isSchoolNotRight(school)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("所填写的学校名称不正确！");
                return replyMessage;
            }
            teacherInfo.setSchool(school);
        }

        teacherInfo.setSex(sex);
        teacherInfo.setGrade(grade);
        teacherInfo.setEdu(edu);

        return teacherInfoService.teacherAlterBaseInfo(teacherInfo);
    }

    /**
     * 教师修改登录名
     */
    @PostMapping("/frame/alterLoginName-submit")
    @ResponseBody
    public ReplyMessage teacherAlterLoginName(@RequestParam(value = "loginName",required = false) String loginName,
                                              HttpServletRequest request){
        HttpSession session = request.getSession();

        ReplyMessage replyMessage = new ReplyMessage();
        if(InputVerifyUtils.isLoginNameNotRight(loginName)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录名称不正确");
            return replyMessage;
        }

        TeacherInfo teacherInfo = (TeacherInfo) session.getAttribute("UserInfo");
        teacherInfo.setName(loginName);
        return teacherInfoService.teacherAlterLoginName(teacherInfo);
    }

    /**
     * 修改教师登录密码
     */
    @PostMapping("/frame/alterPassword-submit")
    @ResponseBody
    public ReplyMessage alterTeacherPasswordSubmit(@RequestParam(value = "oldPassword",required = false) String oldPassword,
                                                   @RequestParam(value = "newPassword",required = false) String newPassword,
                                                   HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        if(InputVerifyUtils.isPasswordNotRight(oldPassword)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("原密码不正确！");
            return replyMessage;
        }
        if(InputVerifyUtils.isPasswordNotRight(newPassword)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("新密码不正确！");
            return replyMessage;
        }

        HttpSession session = request.getSession();
        TeacherInfo teacherInfo = (TeacherInfo) session.getAttribute("UserInfo");
        return teacherInfoService.teacherAlterPassword(oldPassword,newPassword,teacherInfo.getId());
    }

    /**
     * 修改手机号
     */
    @PostMapping("/frame/alterPhone-submit")
    @ResponseBody
    public ReplyMessage teacherAlterPhone(@RequestParam(value = "phone",required = false) String phone,
                                          @RequestParam(value = "imgCode",required = false) String imgCode,
                                          @RequestParam(value = "smsCode",required = false) String smsCode,
                                          HttpServletRequest request){
        HttpSession session = request.getSession();
        ReplyMessage replyMessage = new ReplyMessage();

        if(InputVerifyUtils.isImgAuthCodeNotRight(session,imgCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("图片验证码不正确！");
            return replyMessage;
        }

        if(InputVerifyUtils.isSmsCodeNotRight(session,smsCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("短信验证码不正确！");
            return replyMessage;
        }

        if(InputVerifyUtils.isPhoneNotRight(phone)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("手机号不正确！");
            return replyMessage;
        }

        // 清空图片验证码和短信验证码Session值
        session.removeAttribute("ImgAuthCode");
        session.removeAttribute("SmsAuthCode");

        TeacherInfo teacherInfo = (TeacherInfo) session.getAttribute("UserInfo");
        teacherInfo.setPhone(phone);
        return teacherInfoService.teacherAlterPhone(teacherInfo);
    }

    /**
     * 修改联系信息
     */
    @PostMapping("/frame/alterConnect-submit")
    @ResponseBody
    public ReplyMessage alterPhoneNum(@RequestParam(value = "QQNum",required = false) String qqNum,
                                      @RequestParam(value = "weChatNum",required = false) String weChatNum,
                                      HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();
        TeacherInfo teacherInfo = (TeacherInfo) session.getAttribute("UserInfo");

        if(qqNum != null && qqNum.length() > 0){
            if(InputVerifyUtils.isQQNotRight(qqNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("QQ号不正确！");
                return replyMessage;
            }
            teacherInfo.setQQ(qqNum);
        }

        if(weChatNum != null && weChatNum.length() > 0){
            if(InputVerifyUtils.isWeChatNotRight(weChatNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("微信号不正确！");
                return replyMessage;
            }
            teacherInfo.setWeChat(weChatNum);
        }

        return teacherInfoService.alterConnectInfo(teacherInfo);
    }

    /**
     * 教师查询用户关系
     */
    @PostMapping("/frame/queryAllRelationship")
    @ResponseBody
    public ReplyMessage teacherQueryRelationship(HttpServletRequest request){
        HttpSession session = request.getSession();
        TeacherInfo teacherInfo = (TeacherInfo) session.getAttribute("UserInfo");

        return teacherInfoService.queryAllSelfRelationship(teacherInfo.getId());
    }

    /**
     * 教师添加课程信息
     */
    @PostMapping("/frame/add-class-submit")
    @ResponseBody
    public ReplyMessage teacherAddClassInfo(@RequestParam(value = "relationId",required = false) Integer relationId,
                                            @RequestParam(value = "className",required = false) String className,
                                            @RequestParam(value = "classTime",required = false) String classTime,
                                            @RequestParam(value = "comment",required = false) String comment) {
        ReplyMessage replyMessage = new ReplyMessage();
        ClassInfo classInfo = new ClassInfo();

        if(relationId == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("您尚未选择学生信息！");
            return replyMessage;
        }

        if(className == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("课时名称不能为空！");
            return replyMessage;
        }

        if(className.length() < 1 || className.length() > 40){
            replyMessage.setStatus(400);
            replyMessage.setInfo("课时名称不正确！");
            return replyMessage;
        }

        if(classTime == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("上课时间不能为空！");
            return replyMessage;
        }

        if(classTime.equals("")){
            replyMessage.setStatus(400);
            replyMessage.setInfo("课程时间不正确！");
            return replyMessage;
        }

        if(comment != null){
            if(comment.length() > 40){
                replyMessage.setStatus(400);
                replyMessage.setInfo("课时备注不能超过40个字符！");
                return replyMessage;
            }
            classInfo.setComment(comment);
        }

        classInfo.setRelationId(relationId);
        classInfo.setClassName(className);

        try {
            long haveClassTime = Long.parseLong(classTime);
            classInfo.setClassTime(new Timestamp(haveClassTime));

            return teacherInfoService.teacherAddClassInfo(classInfo);
        } catch (Exception e) {
            e.printStackTrace();

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 教师修改课程信息
     */
    @PostMapping("/frame/alter-class-submit")
    @ResponseBody
    public ReplyMessage alterClassInfo(@RequestParam(value = "className",required = false) String className,
                                       @RequestParam(value = "classTime",required = false) String classTime,
                                       @RequestParam(value = "comment",required = false) String comment,
                                       @RequestParam(value = "classId",required = false) Integer classId){
        ReplyMessage replyMessage = new ReplyMessage();
        ClassInfo classInfo = new ClassInfo();

        if(className == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("课程名称不正确！");
            return replyMessage;
        }

        if(className.length() < 1 || className.length() > 40){
            replyMessage.setStatus(400);
            replyMessage.setInfo("课程名称不正确！");
            return replyMessage;
        }

        if(classTime == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("课程时间不正确！");
            return replyMessage;
        }

        if(classTime.equals("")){
            replyMessage.setStatus(400);
            replyMessage.setInfo("课程时间不正确！");
            return replyMessage;
        }

        try{
            long time = Long.parseLong(classTime);
            classInfo.setClassTime(new Timestamp(time));
        }catch (Exception e){
            e.printStackTrace();

            replyMessage.setStatus(400);
            replyMessage.setInfo("课程时间不正确！");
            return replyMessage;
        }

        if(comment != null){
            if(comment.length() < 1 || comment.length() > 40){
                replyMessage.setStatus(400);
                replyMessage.setInfo("课程备注不正确！");
                return replyMessage;
            }
            classInfo.setComment(comment);
        }

        if(classId == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("无法完成请求，请刷新网页后重新尝试修改！");
            return replyMessage;
        }

        classInfo.setClassId(classId);
        classInfo.setClassName(className);

        return teacherInfoService.alterClassInfo(classInfo);
    }

    /**
     * 教师删除课程信息
     */
    @PostMapping("/frame/delete-class-submit")
    @ResponseBody
    public ReplyMessage deleteClassInfo(@RequestParam(value = "classId",required = false) Integer classId){
        ReplyMessage replyMessage = new ReplyMessage();
        if(classId == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请求参数异常，请尝试重新刷新网页后重试！");
            return replyMessage;
        }

        return teacherInfoService.deleteClassInfo(classId);
    }

    /**
     * 教师修改头像信息
     */
    @PostMapping("/frame/alter-avatar-submit")
    @ResponseBody
    public ReplyMessage teacherAlterAvatar(@RequestParam(value = "avatar",required = false) MultipartFile avatar,
                                           HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();
        TeacherInfo teacherInfo = (TeacherInfo) session.getAttribute("UserInfo");

        if(InputVerifyUtils.isAvatarNotRight(avatar)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请上传正确的图片文件！");
            return replyMessage;
        }

        replyMessage = utilsService.alterAvatar(avatar,teacherInfo.getAvatar(),teacherInfo.getId(),2);
        if(replyMessage.getStatus() == 200){
            teacherInfo.setAvatar(replyMessage.getInfo());
        }

        return replyMessage;
    }

    /**
     * 查询教师头像信息
     */
    @PostMapping("/frame/query-teacher-avatar")
    @ResponseBody
    public ReplyMessage queryTeacherAvatar(HttpServletRequest request){
        HttpSession session = request.getSession();
        TeacherInfo teacherInfo = (TeacherInfo) session.getAttribute("UserInfo");

        ReplyMessage replyMessage = new ReplyMessage();
        replyMessage.setStatus(200);
        replyMessage.setInfo(teacherInfo.getAvatar());
        return replyMessage;
    }

    /**
     * 查询教师当天课程信息
     */
    @PostMapping("/frame/query-teacher-current-class-info")
    @ResponseBody
    public ReplyMessage queryTeacherCurrentClassInfo(HttpServletRequest request){
        HttpSession session = request.getSession();
        TeacherInfo teacherInfo = (TeacherInfo) session.getAttribute("UserInfo");
        return teacherInfoService.queryTeacherCurrentClassInfo(teacherInfo.getId());
    }
}